#include "Laser.h"

Laser::~Laser(void) {
	delete m_Sprites;
}

void Laser::loadContent( ds::ResManager& resourceManager ) {
	m_Sprites = new Vector<ds::SHandle>(10);
	for ( int i = 0; i < 7; ++i ) {
		ds::SHandle handle = add(ds::Vec2(START_X,START_Y + i * 70),ds::Rect(0,180,70,70));
		setVisible(handle,false);
		m_Sprites->set(i,handle);
	}
}

void Laser::update(float elapsed) {
	if ( m_Running ) {
		m_XPos += elapsed * LASER_VELOCITY;
		if ( m_XPos > 900.0f ) {
			m_Running = false;
			for ( int i = 0; i < 7; ++i ) {
				ds::SHandle& sh = m_Sprites->get(i);
				setVisible(sh,false);
			}
			FIRE_EMPTY_EVENT(EVN_LASER_STOPPED);
			gEngine->getAudioManager().stop("Laser");
		}
		for ( int i = 0; i < 7; ++i ) {
			ds::SHandle& sh = m_Sprites->get(i);
			setPosition(sh,ds::Vec2(m_XPos,START_Y + i * 70.0f));
		}
	}
	else {
		m_Timer += elapsed;
		if ( m_Timer > m_Delay ) {
			m_Timer = 0.0f;
			m_Running = true;
			FIRE_EMPTY_EVENT(EVN_LASER_STARTED);
			gEngine->getAudioManager().play("Laser",100,true);
			for ( int i = 0; i < 7; ++i ) {
				ds::SHandle& sh = m_Sprites->get(i);
				setVisible(sh,true);
				m_XPos = START_X - 40.0f;
			}
		}
	}
}
